﻿using Dropbox.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DropboxConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var task = Task.Run((Func<Task>)Program.Run);
            task.Wait();

            var dbx = new DropboxClient("9_bgJRHaxuAAAAAAAAAAg0gZurnOM8y_d3AzeTwwYjM16ipsfSVqQ4gqblIQlOgV");

            //var uploadTask = Program.Upload(dbx, "fold1", "sample1.txt", "my-samplecontent blah...blah...blah....");
            //uploadTask.Wait();

            ListRootFolder(dbx).Wait();

            Upload(dbx, "/", "/", "mycontent.txtasd").Wait();


            Console.WriteLine("DONE");
            Console.ReadLine();
        }

        static async Task Run()
        {
            using (var dbx = new DropboxClient("9_bgJRHaxuAAAAAAAAAAg0gZurnOM8y_d3AzeTwwYjM16ipsfSVqQ4gqblIQlOgV"))
            {
                var full = await dbx.Users.GetCurrentAccountAsync();
                Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email);
            }
        }

        static async Task ListRootFolder(DropboxClient dbx)
        {
            var list = await dbx.Files.ListFolderAsync(string.Empty);

            // show folders then files
            foreach (var item in list.Entries.Where(i => i.IsFolder))
            {
                Console.WriteLine("D  {0}/", item.Name);
            }

            foreach (var item in list.Entries.Where(i => i.IsFile))
            {
                Console.WriteLine("F{0,8} {1}", item.AsFile.Size, item.Name);
            }
        }

        static async Task Upload(DropboxClient dbx, string folder, string file, string content)
        {
            using (var mem = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(content)))
            {
                var updated = await dbx.Files.UploadAsync(
                    "/samples/sample.txt" ,
                    Dropbox.Api.Files.WriteMode.Overwrite.Instance,
                    body: mem);
                Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
            }
        }

    }
}
